package sys.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sys.core.ResponseResult;
import sys.dao.StudentDao;
import sys.dao.StudentTeacherDao;
import sys.entity.*;
import sys.entity.vo.StuCountVo;
import sys.entity.vo.StudentCountVo;
import sys.service.StudentService;
import sys.utils.ExcelUtil;
import sys.utils.PageUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service

public class StudentServiceImpl implements StudentService {

    private static final Logger log = LoggerFactory.getLogger(TeacherServiceImpl.class);
    @Autowired
    private StudentDao dao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StudentTeacherDao studentTeacherDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(StudentEntity entity) {
        //保存学生信息到t_student
        dao.insert(entity);
        Integer studentId = entity.getId();
        String[] ids = entity.getTeacherId().split(",");
        for (String teacherId : ids) {
            StudentTeacherEntity studentTeacherEntity = new StudentTeacherEntity();
            studentTeacherEntity.setStudentId(studentId);
            studentTeacherEntity.setTeacherId(Integer.valueOf(teacherId));
            studentTeacherDao.insert(studentTeacherEntity);
        }
    }

    @Override
    public void delete(StudentEntity entity) {
        dao.deleteById(entity.getId());

    }

    @Override
    public void update(StudentEntity entity) {
        dao.updateById(entity);

    }

    @Override
    public List<StudentEntity> select(StudentEntity entity) {
        return dao.selectList(null);
    }

    @Override
    public PageData<StudentEntity> likeSelect(StudentEntity entity) {
        return null;
    }


  /*  public PageData<StudentEntity> likeSelect(StudentEntity entity) {
        return PageUtil.getPageData(entity, dao);
    }*/


    @Override
    public void exportExcel(StudentEntity entity, HttpServletResponse response) {


        // 获取头部信息（可以设置为动态）
        String[] headList = new String[]{"主键", "学生姓名", "地址", "年龄"};

        String[] headEngList = new String[]{"id", "name", "address", "age"};

        String[] describeList = new String[]{"", "", "", ""};

        //设置头部以及描述信息
        Map<String, String> headAndDescribeMap = new LinkedHashMap<>();
        for (int i = 0; i < headEngList.length; i++) {
            headAndDescribeMap.put(headEngList[i], describeList[i]);
        }

        //ExcelUtil.exportExcel(entity, response, dao, headList, headAndDescribeMap, "积云");


    }

    @Override
    public ResponseResult studentCount() {

        List<StudentCount> maxScore = dao.getMaxScore();
        log.info("总分最高的学生和分数:{}", maxScore);
        List<StudentCount> maxCourseScore = dao.getMaxCourseScore();
        log.info("单科最高的学生和分数:{}", maxCourseScore);
        List<StudentCount> sumStudentScore = dao.getSumStudentScore();
        log.info("每个学生的总分:{}", sumStudentScore);
        StudentCountVo studentCountVo = new StudentCountVo();
        studentCountVo.setMaxScore(maxScore);
        studentCountVo.setMaxCourseScore(maxCourseScore);
        studentCountVo.setSumStudentScore(sumStudentScore);
        return ResponseResult.success(studentCountVo);
    }

    @Override
    public ResponseResult stuCount() {
        //获取总分最高的学生和分数
        List<StuCount> maxSumScore = dao.getMaxSumScore();
        log.info("总分最高的学生和分数:{}", maxSumScore);
        //单科最高的学生和分数
        List<StuCount> maxCourse = dao.getMaxCourse();
        log.info("单科分数最高的学生和分数:{}", maxCourse);
        List<StuCount> totalScore = dao.totalScore();
        log.info("学生总分:{}", totalScore);
        StuCountVo stuCountVo = new StuCountVo();
        stuCountVo.setSumScoreList(maxSumScore);
        stuCountVo.setMaxCourseScoreList(maxCourse);
        stuCountVo.setMaxTotalScoreList(totalScore);

        return ResponseResult.success(stuCountVo);
    }
}
